package data;
import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class POSorderItem extends AbstractData {
	private long id = -1;
	private POSItem	item;
	private String itemString;
	private	String	title;
	private	double	price;
	private	double	discount;
	private	long	amount;
	private	List<POSCalTax> tax;
	private String taxString;
	private	boolean	isCancel;
	private boolean status;
	
	public POSorderItem()
	{
		status = true;
	}
	
	public POSorderItem(long id){
		this.id = id;
		this.status = true;
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public POSItem getItem() {
		if (item == null && !(itemString == null || itemString.isEmpty())) {
			POSItem c = new POSItem(Long.parseLong(itemString));
			c.LoadData();
			item = c;
		}
		return item;
	}
	public void setItem(POSItem item) {
		this.itemString = String.format("%d", item.getId());
		this.item = item;
		this.item = item;
	}
	
	public String getItemString() {
		return itemString;
	}

	public void setItemString(String itemString) {
		this.itemString = itemString;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public double getDiscount() {
		return discount;
	}
	public void setDiscount(double discount) {
		this.discount = discount;
	}
	public long getAmount() {
		return amount;
	}
	public void setAmount(long amount) {
		this.amount = amount;
	}
	public List<POSCalTax> getReloadTax(){
		tax = null;
		return getTax();
	}
	public List<POSCalTax> getTax() {
		if ((tax == null || tax.size() == 0) && !(taxString == null || taxString.isEmpty())) {
			String[] idOfList = SplitList(taxString);
			List<POSCalTax> list = new ArrayList<POSCalTax>();
			for (String id : idOfList) {
				POSCalTax a = new POSCalTax(Long.parseLong(id));
				list.add(a);
			}
			
			for(POSCalTax a : list)
				a.LoadData();
			
			tax = list;
		}
		return tax;
	}
	
	public List<POSCalTax> getTax_Reload() {
		if (!(taxString == null || taxString.isEmpty())) {
			String[] idOfList = SplitList(taxString);
			List<POSCalTax> list = new ArrayList<POSCalTax>();
			for (String id : idOfList) {
				POSCalTax a = new POSCalTax(Long.parseLong(id));
				list.add(a);
			}
			
			for(POSCalTax a : list)
				a.LoadData();
			
			tax = list;
		}
		return tax;
	}

	public void setTax(List<POSCalTax> taxString) {
		this.taxString = CombineList(taxString);
		this.tax = taxString;
	}
	public void setTaxString(String taxString){
		this.taxString = taxString;
	}
	public boolean isCancel() {
		return isCancel;
	}
	public void setCancel(boolean isCancel) {
		this.isCancel = isCancel;
	}
	public boolean isStatus() {
		return status;
	}
	public void setStatus(boolean status) {
		this.status = status;
	}
	
	
	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.POSorderItem);
		else
			item = new Entity(this.key);

		item.setProperty("status", this.status);
		item.setProperty("item", this.itemString);
		item.setProperty("title", this.title);
		item.setProperty("price", this.price);
		item.setProperty("discount", this.discount);
		item.setProperty("amount", this.amount);
		item.setProperty("tax", this.taxString);
		item.setProperty("isCancel", this.isCancel);
		return item;
	}
	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.itemString = (String) entity.getProperty("item");
		this.status = (Boolean) entity.getProperty("status");
		this.title = (String) entity.getProperty("title");
		this.price = (Double) entity.getProperty("price");
		this.discount = (Double) entity.getProperty("discount");
		this.amount = (Long) entity.getProperty("amount");
		this.taxString = (String) entity.getProperty("tax");
		this.isCancel = (Boolean) entity.getProperty("isCancel");
		this.key = entity.getKey();
	}
	
	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.POSorderItem, this.id);
			setEntity(entity);
		}
	}
}
